<?php
/* 
* @Title:  [前台用户模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-13 19:26:21
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-26 16:56:51
* @Copyright:  [hn7m.com]
*/
class UserModel extends Model{
	public $table='user';

	public $validate=array(
		array('uemail','email','邮箱格式不正确',2,3),
		array('uaccount','user:5,20','用户名长度必须大于5小于20且以字母开头', 2,3),
		array('upassword','regexp:/^\w{6,20}$/','密码必须为6-20个字符(不包含空格)',2,3),
		array('upassword','confirm:password_confirm',' 两次密码不一致 ',2,3)
		);
	/**
	 * [check_user 检测用户]
	 * @param  [type] $uaccount [description]
	 * @return [type]           [description]
	 */
	public function check_user($uaccount){
		return $this->where(array('uaccount'=>$uaccount))->getField('uid');
	}
	/**
	 * [check_email 检测邮箱]
	 * @param  [type] $uaccount [description]
	 * @return [type]           [description]
	 */
	public function check_email($uemail){
		return $this->where(array('uemail'=>$uemail))->getField('uid');
	}
	/**
	 * [add_user 注册用户]
	 */
	public function add_user(){
		$data=array(
			'uemail'	=>Q('post.uemail','','htmlspecialchars'),
			'uaccount'	=>Q('post.uaccount','','htmlspecialchars'),
			'upassword'	=>Q('post.upassword','','md5'),
			'uregtime'	=>time()
			);
		return $uid=$this->add($data);
	}
	/**
	 * [login_user 用户登陆]
	 * @return [type] [description]
	 */
	public function login_user(){
		//验证验证码
		if(Q('post.code', '', 'strtoupper') != session('code')){
			$this->error = '验证不正确';
			return false;
		}
		//验证用户名/密码
		$account = Q('post.account','','htmlspecialchars');
		$password = Q('post.password', '', 'md5');
		$where = array(
			'uaccount'	=>	$account
			);

		$userData = $this->where($where)->find();
		if(!$userData || $password != $userData['upassword']){
			$this->error = '用户名或密码不正确';
			return false;
		}
		//是否锁定
		if($userData['uis_lock']==1){
			$this->error = '用户被锁定，请联系管理员';
			return false;
		}

		//走到下面证明全部通过，返回用户所有信息
		return $userData;
	}
	/**
	 * [get_one_data description]
	 * @param  [type] $where [description]
	 * @return [type]        [description]
	 */
	public function get_one_data($where){
		return $this->where($where)->find();
	}
	/**
	 * [get_onedate_value 通过uid获得某个字段值]
	 * @param  [type] $uid [description]
	 * @return [type]      [description]
	 */
	public function get_onedate_value($uid,$field){
		return $this->where(array('uid'=>$uid))->getField($field);
	}
}
?>